package com.htstar.ovms.device.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htstar.ovms.common.core.util.R;
import com.htstar.ovms.common.log.annotation.SysLog;
import com.htstar.ovms.device.api.entity.DeviceAlarmViolation;
import com.htstar.ovms.device.api.vo.AlarmUserNameTestVO;
import com.htstar.ovms.device.service.DeviceAlarmViolationService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 
 *
 * @author htxk
 * @date 2021-02-23 09:15:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/devicealarmviolation" )
@Api(value = "devicealarmviolation", tags = "违规车辆报警管理/Violation vehicle alarm management")
public class DeviceAlarmViolationController {

    private final  DeviceAlarmViolationService deviceAlarmViolationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param deviceAlarmViolation 
     * @return
     */
    @ApiOperation(value = "分页查询/Paging query", notes = "分页查询")
    @GetMapping("/page" )
    public R getDeviceAlarmViolationPage(Page page, DeviceAlarmViolation deviceAlarmViolation) {
        return R.ok(deviceAlarmViolationService.page(page, Wrappers.query(deviceAlarmViolation)));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询/query by id", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(deviceAlarmViolationService.getById(id));
    }

    /**
     * 新增
     *
     * @return R
     */
    @ApiOperation(value = "新增/Add", notes = "新增")
    @GetMapping("/add")
    public R save() {
        List<DeviceAlarmViolation> list = new ArrayList<>();
        for (AlarmUserNameTestVO alarmUserNameTestVO : deviceAlarmViolationService.test()) {
            DeviceAlarmViolation deviceAlarmViolation = new DeviceAlarmViolation();
            deviceAlarmViolation.setUserId(alarmUserNameTestVO.getUserId());
            deviceAlarmViolation.setEtpId(alarmUserNameTestVO.getEtpId());
            deviceAlarmViolation.setDeviceAlarmType(1);
            deviceAlarmViolation.setStaTime(alarmUserNameTestVO.getStaTime());
            deviceAlarmViolation.setEndTime(alarmUserNameTestVO.getStaTime());
            deviceAlarmViolation.setCarListStatus(0);
            deviceAlarmViolation.setLicCode(alarmUserNameTestVO.getLicCode());
            list.add(deviceAlarmViolation);
        }
        return R.ok(deviceAlarmViolationService.saveBatch(list));
    }

    /**
     * 修改
     * @param deviceAlarmViolation 
     * @return R
     */
    @ApiOperation(value = "修改/modify", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_devicealarmviolation_edit')" )
    public R updateById(@RequestBody DeviceAlarmViolation deviceAlarmViolation) {
        return R.ok(deviceAlarmViolationService.updateById(deviceAlarmViolation));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除/Delete by id", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_devicealarmviolation_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(deviceAlarmViolationService.removeById(id));
    }

}
